package org.cloud.scheduler.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.cloud.common.util.StringUtils;
import org.cloud.module.common.dao.ScheduleJobDao;
import org.cloud.module.common.entity.ScheduleJobEntity;
import org.cloud.module.common.model.Query;
import org.cloud.module.common.util.PageUtils;
import org.cloud.scheduler.constant.ScheduleStatus;
import org.cloud.scheduler.service.ScheduleJobService;
import org.cloud.scheduler.util.ScheduleUtils;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service("scheduleJobService")
public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, ScheduleJobEntity> implements ScheduleJobService {
	@Autowired
    private Scheduler scheduler;
	
	/**
	 * 项目启动时，初始化定时器
	 */
	@PostConstruct
	public void init(){
		List<ScheduleJobEntity> scheduleJobList = this.list();
		for(ScheduleJobEntity scheduleJob : scheduleJobList){
			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getId());
            //如果不存在，则创建
            if(cronTrigger == null) {
                ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
            }else {
                ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
            }
		}
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String beanName = (String) params.get("beanName");
		
		QueryWrapper<ScheduleJobEntity> queryWrapper = new QueryWrapper<>();
		if(StringUtils.isNotBlank(beanName)) {
		    queryWrapper.like("bean_name", beanName);
		}
		
		IPage<ScheduleJobEntity> page = new Query<ScheduleJobEntity>().getPage(params);

		page = page(page, queryWrapper);

		return new PageUtils(page);
	}


	@Override
	public void saveJob(ScheduleJobEntity scheduleJob) {
		scheduleJob.setJobStatus(ScheduleStatus.NORMAL.getValue());
        this.save(scheduleJob);
        
        ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
    }
	
	@Override
	public void update(ScheduleJobEntity scheduleJob) {
        ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
                
        this.updateById(scheduleJob);
    }

	@Override
    public void deleteBatch(List<Long> jobIds) {
    	for(Long jobId : jobIds){
    		ScheduleUtils.deleteScheduleJob(scheduler, jobId);
    	}
    	
    	//删除数据
    	this.removeByIds(jobIds);
	}

	@Override
    public int updateBatch(List<Long> jobIds, int jobStatus){
    	Map<String, Object> map = new HashMap<>(2);
    	map.put("list", jobIds);
    	map.put("job_status", jobStatus);
    	return baseMapper.updateBatch(map);
    }
    
	@Override
    public void doRun(List<Long> jobIds) {
    	for(Long jobId : jobIds){
    		ScheduleUtils.run(scheduler, this.getById(jobId));
    	}
    }

	@Override
    public void doPause(List<Long> jobIds) {
        for(Long jobId : jobIds){
    		ScheduleUtils.pauseJob(scheduler, jobId);
    	}
        
    	updateBatch(jobIds, ScheduleStatus.PAUSE.getValue());
    }

	@Override
    public void doResume(List<Long> jobIds) {
    	for(Long jobId : jobIds){
    		ScheduleUtils.resumeJob(scheduler, jobId);
    	}

    	updateBatch(jobIds, ScheduleStatus.NORMAL.getValue());
    }
    
}
